#! /usr/bin/env python
# -*- coding: utf-8 -*-

from playwright.sync_api import Page
from ui_automation_test.common.base_page import BasePage
from ui_automation_test.common.logger import logger
from ui_automation_test.locators.dualPrevention.hiddenDangerTreatment.enterpriseHiddenDanger.loc_enterprise_hidden_danger_add import LocAddEnterpriseHiddenDanger
from ui_automation_test.locators.dualPrevention.hiddenDangerTreatment.enterpriseHiddenDanger.loc_enterprise_hidden_danger_edit import LocEditEnterpriseHiddenDanger
from ui_automation_test.locators.dualPrevention.hiddenDangerTreatment.enterpriseHiddenDanger.loc_enterprise_hidden_danger_view import LocViewEnterpriseHiddenDanger
from ui_automation_test.locators.dualPrevention.hiddenDangerTreatment.enterpriseHiddenDanger.loc_enterprise_hidden_danger_delete import LocDeleteEnterpriseHiddenDanger
from ui_automation_test.locators.dualPrevention.hiddenDangerTreatment.enterpriseHiddenDanger.loc_enterprise_hidden_danger_search import LocSearchEnterpriseHiddenDanger
from ui_automation_test.locators.dualPrevention.hiddenDangerTreatment.enterpriseHiddenDanger.loc_enterprise_hidden_danger_import import LocImportEnterpriseHiddenDanger
from ui_automation_test.locators.dualPrevention.hiddenDangerTreatment.enterpriseHiddenDanger.loc_enterprise_hidden_danger_export import LocExportEnterpriseHiddenDanger


class EnterpriseHiddenDanger(BasePage):   

    def __init__(self, page: Page):
        super().__init__(page)
        self.switch_menu('双重预防机制')
        self.switch_menu('隐患治理闭环管理')
        self.switch_menu('企业隐患自查自改（企业）')       
    

    def add_enterprise_hidden_danger(self, problem_description, rectification_time, fund_input, responsible, measure):
        '''
        新增企业隐患自查自改
        :param problem_description: 问题描述
        :param rectification_time: 整改时间
        :param fund_input: 资金投入(元)
        :param responsible: 责任人
        :param measure: 措施
        '''
        logger.info('点击新增按钮')    
        self.click(LocAddEnterpriseHiddenDanger.loc_add_button)
        logger.info('输入问题描述')
        self.fill(LocAddEnterpriseHiddenDanger.loc_problem_description, problem_description)
        logger.info('输入整改时间')
        self.fill(LocAddEnterpriseHiddenDanger.loc_rectification_time, rectification_time)
        logger.info('输入资金投入(元)')
        self.fill_number(LocAddEnterpriseHiddenDanger.loc_fund_input, fund_input)
        logger.info('输入责任人')
        self.fill(LocAddEnterpriseHiddenDanger.loc_responsible, responsible)
        logger.info('输入措施')
        self.fill(LocAddEnterpriseHiddenDanger.loc_measure, measure)
        logger.info('点击保存按钮')
        self.click(LocAddEnterpriseHiddenDanger.loc_save)

    def edit_enterprise_hidden_danger(self, problem_description=None, rectification_time=None, fund_input=None, responsible=None, measure=None):
        '''
        编辑企业隐患自查自改
        :param problem_description: 问题描述
        :param rectification_time: 整改时间
        :param fund_input: 资金投入(元)
        :param responsible: 责任人
        :param measure: 措施
        '''
        logger.info('点击编辑按钮')
        self.click(LocEditEnterpriseHiddenDanger.loc_edit_button)
        logger.info('输入问题描述')
        if problem_description:
            self.fill(LocEditEnterpriseHiddenDanger.loc_problem_description, problem_description)
        logger.info('输入整改时间')
        if rectification_time:
            self.fill(LocEditEnterpriseHiddenDanger.loc_rectification_time, rectification_time)
        logger.info('输入资金投入(元)')
        if fund_input:
            self.fill_number(LocEditEnterpriseHiddenDanger.loc_fund_input, fund_input)
        logger.info('输入责任人')
        if responsible:
            self.fill(LocEditEnterpriseHiddenDanger.loc_responsible, responsible)
        logger.info('输入措施')
        if measure:
            self.fill(LocEditEnterpriseHiddenDanger.loc_measure, measure)
        logger.info('点击修改按钮')
        self.click(LocEditEnterpriseHiddenDanger.loc_modify)

    def view_enterprise_hidden_danger(self, problem_description):
        '''
        查看企业隐患自查自改
        :param problem_description: 问题描述
        '''
        logger.info('点击查看按钮')
        self.click_table_button_by_row(0, 9, '查 看', problem_description)


    def delete_enterprise_hidden_danger(self, problem_description):
        '''
        删除企业隐患自查自改
        :param problem_description: 问题描述    
        '''
        logger.info('点击删除按钮')
        self.click_table_button_by_row(0, 9, '删 除', problem_description)
        logger.info('点击确定按钮')
        self.click(LocDeleteEnterpriseHiddenDanger.loc_confirm_delete_button)

    def search_enterprise_hidden_danger(self, enterprise=None, problem_description=None, rectification_time_start=None, rectification_time_end=None):
        '''
        搜索企业隐患自查自改
        :param enterprise: 所属企业
        :param problem_description: 问题描述
        :param rectification_time_start: 整改时间开始
        :param rectification_time_end: 整改时间结束
        '''
        logger.info('选择所属企业')
        if enterprise:
            self.select(LocSearchEnterpriseHiddenDanger.loc_enterprise, enterprise)
        logger.info('输入问题描述')
        if problem_description:
           self.fill(LocSearchEnterpriseHiddenDanger.loc_problem_description, problem_description)
        logger.info('选择整改时间')
        if rectification_time_start and rectification_time_end:
            self.fill_date_time(LocSearchEnterpriseHiddenDanger.loc_rectification_time, rectification_time_start, rectification_time_end)
        logger.info('点击查询按钮')
        self.click(LocSearchEnterpriseHiddenDanger.loc_search_button)
    
    def import_enterprise_hidden_danger(self, file_path):
        '''
        导入企业隐患自查自改
        :param file_path: 文件路径
        '''
        logger.info('点击导入按钮')
        self.click(LocImportEnterpriseHiddenDanger.loc_import_button)
        logger.info('上传文件')
        self.upload_file(file_path, loc=LocImportEnterpriseHiddenDanger.loc_upload_button)

    def export_enterprise_hidden_danger(self, problem_description):
        '''
        导出企业隐患自查自改
        :param import_problem_description: 导入的问题描述
        '''
        logger.info('勾导入的数据')
        self.select_row_checkbox(problem_description)
        self.page.wait_for_timeout(1000)
        logger.info('导出文件')
        self.export_and_download(LocExportEnterpriseHiddenDanger.loc_export_button, 'excel')